version 18.0               // version control
set processors 8           // to ensure replicability across different numbers of cores
clear all                  // clear existing data
macro drop _all            // and macros, clean slate
set seed 20220801          // set seed

*-------------------------------------------------------
* project: daylight saving time (dst)
*-------------------------------------------------------

local pgm  "dst-data04_for_estimation_within_250_miles" // file name
local who  "Muzhe Yang"                                 // author
local dte  "2022-08-01"                                 // created date
local dte2 "`c(current_date)'"                          // last run date
local tag  "`pgm'.do, created by `who' on `dte', last run on `dte2'"

capture log close
log using "code\management\\`pgm'.txt", replace text
display "`tag'"

* Step 1: combine datasets -----------------------------------------------------------------

local radius = 250
foreach x in eastern_central central_mountain mountain_pacific {
	use "data_clean\dst-data03_for_estimation_`x'", clear
	keep if dist_to_border <= `radius'
	tab time_zone, missing
	gen     region = "eastern and central"  if "`x'" == "eastern_central"
	replace region = "central and mountain" if "`x'" == "central_mountain"
	replace region = "mountain and pacific" if "`x'" == "mountain_pacific"
	order region, before(time_zone)
	compress
	save "data_clean\\`x'_within_`radius'_miles", replace 
}
use "data_clean\eastern_central_within_`radius'_miles", clear
append using "data_clean\central_mountain_within_`radius'_miles"
append using "data_clean\mountain_pacific_within_`radius'_miles"
codebook TractFIPS if wave == 1
codebook TractFIPS if wave == 2
drop if TractFIPS == "30027030100" & region == "central and mountain"
* This "30027030100" census tract is in Fergus County of Montana. 
* Its centroid is 244.76543 miles away from the MT/PT border, and 247.07551 miles away from the CT/MT border.
* To ensure we don't use the same census tract for different regions, we use this "30027030100" only for the "mountain and pacific" region. 
codebook TractFIPS if wave == 1
codebook TractFIPS if wave == 2
label variable region "two adjacent time zones"

* Step 2: create new variables --------------------------------------------------------------

gen     treat = 1 if time_zone == "Eastern"  & region == "eastern and central"
replace treat = 0 if time_zone == "Central"  & region == "eastern and central"
replace treat = 1 if time_zone == "Central"  & region == "central and mountain"
replace treat = 0 if time_zone == "Mountain" & region == "central and mountain"
replace treat = 1 if time_zone == "Mountain" & region == "mountain and pacific"
replace treat = 0 if time_zone == "Pacific"  & region == "mountain and pacific"
label variable treat "1 = east of the time zone border; 0 = west of the time zone border"
order treat, after(time_zone)

gen double     dist_to_border_signed = (2*treat - 1)*dist_to_border
label variable dist_to_border_signed "dist_to_border: positive for treat = 1, negative for treat = 0"
order dist_to_border_signed, after(dist_to_border)

gen double daylight_dur_range = daylight_dur_max - daylight_dur_min
label variable daylight_dur_range "daylight_dur_max - daylight_dur_min" 
order daylight_dur_range, after(daylight_dur_max)

* Step 3: final cleanup ------------------------------------------------------------------------------------------------------------------

sort TractFIPS wave
codebook, compact
compress
save "data_clean\\`pgm'.dta", replace 
erase "data_clean\eastern_central_within_`radius'_miles.dta"
erase "data_clean\central_mountain_within_`radius'_miles.dta"
erase "data_clean\mountain_pacific_within_`radius'_miles.dta"

log close
exit